<?php

namespace app\admin\controller;
use app\admin\model\Node;
use app\admin\model\OrganizationModel;
use think\Db;

class Organization extends Base
{

    /**
     * [index 单位列表]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function index(){

        $nav = new \org\Leftnav;
        $org = new OrganizationModel();
        $admin_organization = $org->getAllOrganizations("");
        $arr = $nav::rule($admin_organization);
        $this->assign('admin_organization',$arr);
        return $this->fetch();
    }



    /**
     * [organizationAdd 添加单位]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function organizationAdd()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $org = new OrganizationModel();
            $flag = $org->insertOrganization($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        return $this->fetch();
    }



    /**
     * [organizationEdit 编辑单位]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function organizationEdit()
    {
        $org = new OrganizationModel();
        if(request()->isAjax()){
            $param = input('post.');
            $flag = $org->editOrganization($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        $this->assign([
            'organization' => $org->getOneOrganization($id)
        ]);
        return $this->fetch();
    }



    /**
     * [organizationDel 删除单位]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function organizationDel()
    {
        $id = input('param.id');
        $organization = new OrganizationModel();
        $flag = $organization->delOrganization($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }


 /**
     * [ruleorder 排序]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function organizationOrder()
    {
        if (request()->isAjax()){
            $param = input('post.');     
            $auth_organization = Db::name('organization');
            foreach ($param as $id => $sort){
                $auth_organization->where(array('id' => $id ))->setField('sort' , $sort);
            }
            return json(['code' => 1, 'msg' => '排序更新成功']);
        }
    }

    /**
     * [organization_state 单位状态]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function organizationState()
    {
        $id = input('param.id');
        $status = Db::name('organization')->where('id',$id)->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('organization')->where('id',$id)->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('organization')->where('id',$id)->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }
    
    }


}